using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.TFL.Base;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_setview : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		//bool letView = false;
		bool isLoggedIn = false;
		//bool isInSet = false;
		int line = 0;

		TableRow row = null;
		Account tmp = new Account();

		if(ip.CheckSetExists(Request.QueryString.Get("set"))) {
			if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
				tmp = ap.GetAccountBySession(Session["Zappr"].ToString());
				isLoggedIn = true;
			}

			if(Request.QueryString.Get("set") != null) {
				HttpBrowserCapabilities hbc = Request.Browser;

				if(hbc.Type == "IE"
				|| hbc.Type == "IE5"
				|| hbc.Type == "IE6"
				|| hbc.Type == "IE7") {
					tableSet.CellSpacing = 5;
				}

				SetItem set = ip.GetSet(Convert.ToInt64(Request.QueryString.Get("set")), true);
				Account acc = ap.GetAccount(set.UserID);

				setTitle.Text = set.Name;
				setText.Text = set.Description;

				// creator
				Label text = new Label();
				text.CssClass = "listFooter";
				text.Text = GetLocalResourceObject("CreateBy").ToString();
				text.Text += " ";

				plcCreator.Controls.Add(text);

				HyperLink creator = new HyperLink();
				creator.CssClass = "listFooter";
				creator.Text = acc.Username;
				//creator.NavigateUrl = "~/default.aspx?id=images&user=" + acc.Username;
				creator.NavigateUrl = "~/images/" + acc.Username + "/default.aspx";

				plcCreator.Controls.Add(creator);

				// main image
				if(set.Thumbnail == -1) {
					Image maintmp = new Image();
					maintmp.ImageUrl = "~/App_Themes/Default/Images/placeholder.jpg";

					panelSetMain.Controls.Add(maintmp);
				}
				else {
					ImageItem mainimg = ip.GetImage(Convert.ToInt64(set.Thumbnail));
					//ImageItem mainimg = ip.GetImageByImageID(set.Thumbnail.Replace("_75x75.jpg", ""));

					HyperLink main = new HyperLink();
					//main.NavigateUrl = "~/default.aspx?item=" + mainimg.ImageID.ToString();
					main.NavigateUrl = "~/photo/" + mainimg.ImageID.ToString() + "/default.aspx";
					main.ImageUrl = "~/Images/" + mainimg.Image240;

					panelSetMain.Controls.Add(main);
				}

				panelSetMain.Controls.Add(new LiteralControl("<br />"));
				panelSetMain.Controls.Add(new LiteralControl("<br />"));

				Label amount = new Label();
				amount.CssClass = "listFooter";
				amount.Text = GetLocalResourceObject("AmountOfFotos").ToString();
				amount.Text += " " + set.Images.Count.ToString() + " ";
				amount.Text += GetLocalResourceObject("Photos").ToString();
				amount.Style.Add("padding-top", "20px");

				panelSetMain.Controls.Add(amount);

				// actions
				if(isLoggedIn && tmp.ID == acc.ID) {
					// change
					panelSetMain.Controls.Add(new LiteralControl("<br />"));
					panelSetMain.Controls.Add(new LiteralControl("<br />"));

					HyperLink change = new HyperLink();
					change.CssClass = "listFooter";
					change.Text = GetLocalResourceObject("Change").ToString();
					change.NavigateUrl = "~/default.aspx?id=_editset&set=" + set.SetID.ToString();

					panelSetMain.Controls.Add(change);

					// delete
					panelSetMain.Controls.Add(new LiteralControl("<br />"));

					LinkButton del = new LinkButton();
					del.CssClass = "listFooter";
					del.Text = GetLocalResourceObject("Delete").ToString();
					del.OnClientClick = "javascript:chk=confirm('"
						+ GetLocalResourceObject("DeleteThis").ToString()
						+ "');return chk;";
					del.Command += new CommandEventHandler(btnDelete_Click);
					del.CommandName = "btnDelete_Click";
					del.CommandArgument = set.ID + "$" + acc.Username;

					panelSetMain.Controls.Add(del);
				}

				// image list
				foreach(ImageItem item in set.Images) {
					if(line == 0) {
						row = new TableRow();
					}

					TableCell cell = new TableCell();
					cell.VerticalAlign = VerticalAlign.Top;
					cell.Width = new Unit(78);
					cell.Style.Add("width", "78px");

					HyperLink img = new HyperLink();
					//img.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
					img.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";
					img.ImageUrl = "~/Images/" + item.Image75x75;

					cell.Controls.Add(img);

					row.Cells.Add(cell);
					tblList.Rows.Add(row);

					line++;

					if(line == 7) {
						row = null;
						line = 0;
					}
				}
			}
			else {
				Response.Redirect("~/default.aspx");
			}
		}
		else {
			Label not = new Label();
			not.CssClass = "txtNormal";
			not.Text = GetLocalResourceObject("SetDoesNotExists").ToString();

			panelSetList.Controls.Add(not);
		}
	}

	/// <summary>
	/// btnDelete_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnDelete_Click(object sender, CommandEventArgs e) {
		char[] seperator = new char[1];
		seperator[0] = '$';
		string[] spliter = e.CommandArgument.ToString().Split(seperator);

		ip.DeleteSet(spliter[0]);

		//Response.Redirect("~/default.aspx?id=images&user=" + spliter[0]);
		Response.Redirect("~/images/" + spliter[0] + "/default.aspx");
	}
}
